/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meuevento.controller;

import com.meuevento.business.event.Event;
import com.meuevento.business.event.EventService;
import com.meuevento.business.user.User;
import com.meuevento.business.user.UserService;
import com.meuevento.util.ErrorManager;
import com.meuevento.util.Populator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Fernando Arena Varella (favarella@inf.ufrgs.br)
 */
@ManagedBean
@RequestScoped
public class UserMB {

    @EJB UserService userService;
    @EJB EventService eventService;
    @EJB Populator populator;

    private List<User> users;

    private User user;

    private Event selectedEvent;

    public void listUsersOnServer(){
        System.out.println("calling ejb");
        userService.listUsers();
    }

    public String selectUser(User user){
        this.user = user;
        return "viewUser";
    }

    public List<User> getUsers() {
        if (users == null)
            users = userService.listUsers();
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Event getSelectedEvent() {
        return selectedEvent;
    }

    public void setSelectedEvent(Event selectedEvent) {
        this.selectedEvent = selectedEvent;
    }



    public String removeEvent(){
        Event selected = selectedEvent;
        if(selected == null){
            ErrorManager.generateError("evento inexistente", "");
        } else{
            //Event e = eventService.findById(selected.getEventId());
            try {
                eventService.remove(selected);
            } catch (Exception ex) {
               ex.printStackTrace();
               ErrorManager.generateError(ex.getMessage(), "problemas ao remover evento:"+selected);
            }
        }
        return null;
    }

}
